package
{
    import flash.display.*;
    import flash.events.Event;
    import flash.geom.*;
    import flash.text.*;

    [SWF(width="640", height="360", backgroundColor="#FFFFFF")]
    public class CircleFlatWhite extends Sprite
    {
        private var A:int;
        private var TA:int;
        private var R:Number;
        private var T:int;
        private var D:Boolean;
        private const times:int = 2;
        private var pen:Sprite;

        public function CircleFlatWhite()
        {
            x = 320;
            y = 180;
            A = 0;
            TA = 0;
            R = 0;
            T = 2;
            D = true;

            pen = new Sprite();
            var colors:Array = [0xFFFFFF, 0xFF0000];
            var alphas:Array = [1, 0];
            var ratios:Array = [63, 255];
            var matr:Matrix = new Matrix();
            matr.createGradientBox(20, 20, 0, -10, -10);
            pen.graphics.beginGradientFill(GradientType.RADIAL,
                                           colors, alphas,
                                           ratios, matr);
            pen.graphics.drawCircle(0, 0, 10);
            pen.graphics.endFill();
            addChild(pen);

            addEventListener(Event.ENTER_FRAME, update);
        }

        public function randomAngle():int
        {
            return int(Math.random() * 180) * 2;
        }

        public function randomDirection():Boolean
        {
            var a:Number = Math.random();
            if(a > 0.50) return true;
            return false;
        }

        public function getRadia(a:int):Number
        {
            return Math.PI * Number(a) / 180;
        }

        public function reset():void
        {
            if(T >= times)
            {
                T = 0;
                graphics.clear();
                graphics.lineStyle(2, 0x000000, 0.5);
            }
            next();
        }

        public function next():void
        {
            T++;
            R = 150;
            A = randomAngle();
            TA = randomAngle();
            D = randomDirection();
            var r:Number = getRadia(A);
            var px:Number = R * Math.cos(r);
            var py:Number = R * Math.sin(r);
            graphics.moveTo(px, py);
            pen.x = px;
            pen.y = py;
        }

        public function display():void
        {
            var r:Number = getRadia(A);
            var px:Number = R * Math.cos(r);
            var py:Number = R * Math.sin(r);
            graphics.lineTo(px, py);
            pen.x = px;
            pen.y = py;
        }

        public function update(e:Event):void
        {
            if(A != TA)
            {
                if(D == true)
                {
                    A = (A + 2) % 360;
                    rotation += 1;
                }
                else
                {
                    A = (A + 358) % 360;
                    rotation -= 1;
                }
                display();
            }
            else if(R > 20)
            {
                R -= 20;
                var r:Number = getRadia(A);
                graphics.lineTo(R*Math.cos(r), R*Math.sin(r));
                TA = int(randomAngle());
                D = randomDirection();
            }
            else
            {
                reset();
            }
        }
    }
}
